package com.winit.deploy.task.tomcatTask;

import com.winit.common.util.ConfigUtil;
import com.winit.common.util.HttpClientUtil;
import com.winit.common.util.RedisUtil;
import com.winit.core.server.base.CmdOutputHandler;
import com.winit.core.task.Task;
import com.winit.deploy.bean.WinitTomcatInfo;
import com.winit.deploy.server.WinitTomcatServer;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * Created by wangjian on 16/8/10.
 * 发布任务step5 启动server
 */
public class DeployServerCheckTask extends Task {

    static final Logger logger = LoggerFactory.getLogger(DeployServerCheckTask.class);

    private WinitTomcatServer winitTomcatServer;

    public DeployServerCheckTask(String taskId, Integer expireTime, WinitTomcatServer winitTomcatServer) {
        super(taskId, expireTime);
        this.winitTomcatServer = winitTomcatServer;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call start taskId=" + this.taskId);
        WinitTomcatInfo tomcatInfo = this.winitTomcatServer.getWinitTomcatInfo();
        String server_start_check = ConfigUtil.getValue("server_start_check");
        TaskStatus status = TaskStatus.STATUS_ERROR;
        if (ConfigUtil.getValue("server_start_check_env").contains("," + tomcatInfo.getEnvName() + ",") && !StringUtils.isEmpty(server_start_check)
                && "true".equals(server_start_check)) {
            Integer time = StringUtils.isEmpty(ConfigUtil.getValue("server_start_check_time")) ? 10000 : Integer.parseInt(ConfigUtil.getValue("server_start_check_time"));

            //时间耗尽前 循环检查服务器状态
            while (time > 0) {
                logger.info("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call start check taskId=" + this.taskId);
                if (doCheck(tomcatInfo)) {
                    status = TaskStatus.STATUS_SUCCESS;
                    break;
                }
                Thread.sleep(10000);
                time = time - 10000;
            }

        } else {
            status = TaskStatus.STATUS_SUCCESS;
        }
        logger.info("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call end taskId=" + this.taskId);
        return status;
    }

    private boolean doCheck(WinitTomcatInfo tomcatInfo) {
        String url = "http://" + tomcatInfo.getServerIp() + ":" + tomcatInfo.getHttpPort() + "/" + tomcatInfo.getWarName();
        try {
            logger.info("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call start doget taskId=" + this.taskId);
            HttpClientUtil.doGet(url, Integer.parseInt(ConfigUtil.getValue("server_start_check_timeout")));
            logger.info("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call start doget end taskId=" + this.taskId);
            return true;
        } catch (Exception e) {
            logger.error("com.winit.deploy.task.tomcatTask.DeployServerCheckTask.call 启动服务,调用服务异常," + e.getMessage() + ",url=" + url);
            return false;
        }
    }
}
